package com.enba.mallapi.module.dict.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.enba.boot.core.exception.BizException;
import com.enba.mallapi.module.dict.controller.req.SysDictAddUpdateReq;
import com.enba.mallapi.module.dict.controller.req.SysDictItemAddUpdateReq;
import com.enba.mallapi.module.dict.entity.SysDict;
import com.enba.mallapi.module.dict.entity.SysDictItem;
import com.enba.mallapi.module.dict.mapper.SysDictItemMapper;
import com.enba.mallapi.module.dict.mapper.SysDictMapper;
import com.enba.mallapi.module.dict.service.SysDictService;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict>
    implements SysDictService {

  @Autowired private SysDictItemMapper sysDictItemMapper;

  @Override
  public SysDictItemMapper getSysDictItemMapper() {
    return sysDictItemMapper;
  }

  @Override
  public Page<SysDict> listByCondition(Page<SysDict> page, String dictName, Integer status) {
    LambdaQueryWrapper<SysDict> queryWrapper =
        Wrappers.<SysDict>lambdaQuery().orderByDesc(SysDict::getCreateTime);

    queryWrapper.eq(SysDict::getDelFlag, 0);

    if (StringUtils.isNotBlank(dictName)) {
      queryWrapper.like(SysDict::getDictName, dictName);
    }

    if (status != null) {
      queryWrapper.eq(SysDict::getStatus, status);
    }

    return this.page(page, queryWrapper);
  }

  @Override
  public boolean saveOrUpdate(SysDictAddUpdateReq req) {
    // 字典编码唯一
    if (checkDictCode(req.getDictCode(), req.getId())) {
      BizException.throwEx("字典编码已存在");
    }

    if (req.getId() == null) {
      // 新增
      SysDict sysDict = new SysDict();
      sysDict.setDictName(req.getDictName());
      sysDict.setDictCode(req.getDictCode());
      sysDict.setDescription(req.getDescription());
      sysDict.setStatus(req.getStatus());
      return save(sysDict);
    }

    // 编辑
    SysDict sysDict = getById(req.getId());
    if (Objects.isNull(sysDict)) {
      BizException.throwEx("字典不存在");
    }

    sysDict.setDictName(req.getDictName());
    sysDict.setDictCode(req.getDictCode());
    sysDict.setDescription(req.getDescription());
    sysDict.setStatus(req.getStatus());
    return updateById(sysDict);
  }

  private boolean checkDictCode(String dictCode, Long id) {

    return count(
            new LambdaQueryWrapper<SysDict>()
                .eq(SysDict::getDictCode, dictCode)
                .ne(Objects.nonNull(id), SysDict::getId, id))
        > 0;
  }

  @Override
  public boolean delete(Long id) {
    // 检查是否有字典项
    if (sysDictItemMapper.selectCount(
            Wrappers.<SysDictItem>lambdaQuery().eq(SysDictItem::getDictId, id))
        > 0) {
      BizException.throwEx("请先删除字典项");
    }

    return this.removeById(id);
  }

  @Override
  public Page<SysDictItem> listItemByCondition(
      Page<SysDictItem> page, Long dictId, String itemText, Integer status) {
    LambdaQueryWrapper<SysDictItem> queryWrapper =
        Wrappers.<SysDictItem>lambdaQuery().orderByDesc(SysDictItem::getCreateTime);
    queryWrapper.eq(SysDictItem::getDictId, dictId);

    if (status != null) {
      queryWrapper.eq(SysDictItem::getStatus, status);
    }

    return sysDictItemMapper.selectPage(page, queryWrapper);
  }

  @Override
  public boolean saveOrUpdate(SysDictItemAddUpdateReq req) {
    Long id = req.getId();

    if (id == null) {
      if (Objects.isNull(req.getDictId())) BizException.throwEx("字典ID不能为空");
      // 新增逻辑
      SysDictItem sysDictItem = new SysDictItem();
      sysDictItem.setDictId(req.getDictId());
      sysDictItem.setItemText(req.getItemText());
      sysDictItem.setItemValue(req.getItemValue());
      sysDictItem.setDescription(req.getDescription());
      sysDictItem.setSortOrder(req.getSortOrder());
      sysDictItem.setStatus(req.getStatus());
      return sysDictItemMapper.insert(sysDictItem) > 0;
    }

    // 编辑
    SysDictItem sysDictItem = sysDictItemMapper.selectById(id);
    if (Objects.isNull(sysDictItem)) {
      BizException.throwEx("字典项不存在");
    }

    sysDictItem.setItemText(req.getItemText());
    sysDictItem.setItemValue(req.getItemValue());
    sysDictItem.setDescription(req.getDescription());
    sysDictItem.setSortOrder(req.getSortOrder());
    sysDictItem.setStatus(req.getStatus());
    return sysDictItemMapper.updateById(sysDictItem) > 0;
  }
}
